package com.zsrt.oa.employee.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zsrt.common.entity.Combobox;
import com.zsrt.common.entity.LeftMenuEntity;
import com.zsrt.oa.dept.dao.DeptDao;
import com.zsrt.oa.dept.dao.impl.DeptDaoImpl;
import com.zsrt.oa.employee.dao.EmployeeDao;
import com.zsrt.oa.employee.dao.impl.EmployeeDaoImpl;
import com.zsrt.oa.employee.entity.EmployeeEntity;
import com.zsrt.oa.employee.service.EmployeeService;
import com.zsrt.oa.hobby.dao.HobbiesDao;
import com.zsrt.oa.hobby.dao.impl.HobbiesDaoImpl;
import com.zsrt.oa.hobby.entity.HobbiesEntity;

public class EmployeeServiceImpl implements EmployeeService{
	
	/**
	 * 员工接口
	 */
	private EmployeeDao dao = new EmployeeDaoImpl();
	
	/**
	 * 爱好接口
	 */
	private HobbiesDao hobbiesDao = new HobbiesDaoImpl();
	
	/**
	 * 部门接口
	 */
	private DeptDao deptDao = new DeptDaoImpl();
	
	@Override
	public void add(EmployeeEntity entity,Map<String, Object> map,String empHobbysArrays) {
		int result = 0;
		try {
			int empId = dao.add(entity);
			
			String[] arr = empHobbysArrays.split(",");
			HobbiesEntity hobbiesEntity = new HobbiesEntity();
			hobbiesEntity.setHobbysId(arr);
			hobbiesEntity.setEmpId(empId);
			hobbiesDao.add(hobbiesEntity);
			
			result = 1;
			map.put("result", "success");
			map.put("message", "增加成功");
			map.put("data", result);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "增加失败");
			map.put("data", result);
			e.printStackTrace();
		}
	}
	
	/**
	 * 分页查询
	 * @param min 从第几条开始显示
	 * @param max 第几条结束
	 * @return
	 */
	@Override
	public void query(String pageSize,String curPage,Map<String, Object> map){
		List<EmployeeEntity> list = new ArrayList<EmployeeEntity>();
		
		if(null == pageSize || "".equals(pageSize)){
			pageSize = "1";
		}
		int ps = Integer.parseInt(pageSize);
		
		if(null == curPage || "".equals(curPage)){
			curPage = "5";
		}
		int cp = Integer.parseInt(curPage);
		
		try {
			list = dao.query(ps,cp,map);
			map.put("result", "success");
			map.put("message", "查询成功");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "查询失败");
			map.put("data", list);
			e.printStackTrace();
		}
	}
	
	/**
	 * 员工增加部门显示下拉框
	 * @param map
	 * @throws SQLException
	 */
	public void deptQuery(Map<String, Object> map){
		List<Combobox> list = new ArrayList<Combobox>();
		try {
			list = deptDao.deptQuery();
			map.put("result", "success");
			map.put("message", "查询成功");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "查询失败");
			map.put("data", list);
			e.printStackTrace();
		}
	}
	
	/**
	 * 爱好动态生成
	 * @param map
	 * @throws SQLException
	 */
	@Override
	public void empHobby(Map<String, Object> map){

		List<Combobox> list = new ArrayList<Combobox>();
		try {
			list = hobbiesDao.empHobby();
			map.put("result", "success");
			map.put("message", "查询成功");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "查询失败");
			map.put("data", list);
			e.printStackTrace();
		}
	}
	
	/**
	 * 员工批量删除
	 * @param entity
	 * @param map
	 */

	@Override
	public void empDeletebByIds(EmployeeEntity entity, Map<String, Object> map) {
		try {
			HobbiesEntity hobbiesEntity = new HobbiesEntity();
			String empId = entity.getEmpId();
			
			hobbiesEntity.setHobbysId(empId.split(","));
			
			//首先删除员工
			dao.empDeletebByIds(entity);
			
			//再删除爱好
			hobbiesDao.hobbiesDeleteByIds(hobbiesEntity);
			map.put("result", "success");
			map.put("message", "删除成功");
			map.put("data", 1);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "删除失败");
			map.put("data", 0);
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 根据id进行查询
	 * @param entity
	 * @throws SQLException
	 */
	public void empFindById(EmployeeEntity entity,Map<String, Object> map){
		try {
			dao.empFindById(entity);
			
			List<Combobox> list = hobbiesDao.hobbiesFindById(entity.getEmpId());
			map.put("result", "success");
			map.put("message", "查询成功");
			map.put("data", entity);
			map.put("hobbiesData", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "查询失败");
			e.printStackTrace();
		}
	}
	
	/**
	 * 员工修改
	 * @param entity
	 * @param map
	 */
	@Override
	public void empUpdate(EmployeeEntity entity, Map<String, Object> map,String empHobbysArrays) {
		
		try {
			dao.empUpdate(entity);
			HobbiesEntity hobbiesEntity = new HobbiesEntity();
			String empId = entity.getEmpId();
			hobbiesEntity.setHobbysId(empId.split(","));
			hobbiesEntity.setEmpId(Integer.parseInt(entity.getEmpId()));
			
			//首先删除爱好表
			hobbiesDao.hobbiesDeleteByIds(hobbiesEntity);
			hobbiesEntity.setHobbysId(empHobbysArrays.split(","));
			
			//再重新新增
			hobbiesDao.add(hobbiesEntity);
			
			map.put("result", "success");
			map.put("message", "修改成功");
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "查询失败");
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 用户登录校验
	 * @param empPhone
	 * @param empPassword
	 * @param map
	 */
	@Override
	public void login(String empPhone, String empPassword, Map<String, Object> map) {
		EmployeeEntity entity = new EmployeeEntity();
		entity.setEmpPhone(empPhone);
		entity.setEmpPassword(empPassword);
		try {
			int counter = dao.login(entity);
			if(counter == 1){
				map.put("result", "success");
				map.put("message", "登录成功...");
			}else{
				map.put("result", "error");
				map.put("message", "账号密码输入有误，请重新输入...");
			}
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "服务器内部出错，程序员拼命在抢修..请稍候再试..");
			e.printStackTrace();
		}
	}
	
	/**
	 * 自动生成左菜单
	 * @param map
	 */
	@Override
	public void leftMenuAppend(Map<String, Object> map) {
		List<LeftMenuEntity> list = new ArrayList<LeftMenuEntity>();
		try {
			list = dao.leftMenuAppend(map);
			map.put("result", "success");
			map.put("message", "查询成功...");
			map.put("data", list);
			
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "查询失败");
			map.put("data", list);
			e.printStackTrace();
		}
	}
}
